const gulp = require("gulp");
const babel = require("gulp-babel");
const webserver = require("gulp-webserver");
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');

sass.compiler = require('node-sass');

gulp.task("server", ["static"], function(){
	
	gulp.src("dist")
		.pipe(webserver({
			
			livereload : true,
			proxies : [
					{
						source : "/login/code",
						target : "http://v.juhe.cn/sms/send"
						// target : "https://shopapi.smartisan.com/product/home"
					},
					
					{
						source : "/goodsInfo/priceInfo/",
						target : "https://www.iliangcang.com/i/goods/"
					}
			]
		}));
		
		gulp.watch("pages/**/*.html",["compileHTML"]);
		
		gulp.watch("scripts/**/*.js",["compileJS"]);
		
		gulp.watch("styles/**/*.scss",["compileSASS"]);
		
		gulp.watch("conf/*.js",["compileConf"]);
		
		gulp.watch("source/**/*.*",["source"]);
	
})
gulp.task("compileConf", function(){
	
	gulp.src("conf/*.js")
		.pipe(gulp.dest("dist/conf/"));
	
})
gulp.task("compileHTML", function(){
	
	gulp.src("pages/**/*.html")
		.pipe(gulp.dest("dist/pages/"));
	
})
gulp.task("compileJS", function(){
	
	gulp.src("scripts/**/*.js")
		.pipe( babel({
			presets : ['@babel/env']
		}))
		.pipe(uglify())
		.pipe( gulp.dest("dist/scripts/"));
})

gulp.task("compileSASS", function(){
	
	gulp.src("styles/**/*.scss")
		.pipe(sass().on('error', sass.logError))
		.pipe(gulp.dest('dist/styles/'));
})

gulp.task("static", function(){
	
	gulp.src("static/**/*.*")
		.pipe(gulp.dest("dist/static/"));
	
})

gulp.task("source", function(){
	
	gulp.src("source/**/*.*")
		.pipe(gulp.dest("dist/source/"));
	
})